/*
 *  Copyright 2006 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.mybatis.generator.api.dom;

import java.util.Set;
import java.util.TreeSet;

import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;

/**
 * @author Jeff Butler
 */
public class OutputUtilities {
	public static final String lineSeparator;

	static {
		String ls = System.getProperty("line.separator"); //$NON-NLS-1$
		if (ls == null) {
			ls = "\n"; //$NON-NLS-1$
		}
		lineSeparator = ls;
	}

	/**
	 * Utility class - no instances allowed
	 */
	private OutputUtilities() {
		super();
	}

	/**
	 * Utility method that indents the buffer by the default amount for Java
	 * (four spaces per indent level).
	 * 
	 * @param sb
	 *            a StringBuilder to append to
	 * @param indentLevel
	 *            the required indent level
	 */
	public static void javaIndent(StringBuilder sb, int indentLevel) {
		for (int i = 0; i < indentLevel; i++) {
			sb.append("    "); //$NON-NLS-1$
		}
	}

	/**
	 * Utility method that indents the buffer by the default amount for XML (two
	 * spaces per indent level).
	 * 
	 * @param sb
	 *            a StringBuilder to append to
	 * @param indentLevel
	 *            the required indent level
	 */
	public static void xmlIndent(StringBuilder sb, int indentLevel) {
		for (int i = 0; i < indentLevel; i++) {
			sb.append("  "); //$NON-NLS-1$
		}
	}

	/**
	 * Utility method. Adds a newline character to a StringBuilder.
	 * 
	 * @param sb
	 *            the StringBuilder to be appended to
	 */
	public static void newLine(StringBuilder sb) {
		sb.append(lineSeparator);
	}

	/**
	 * returns a unique set of "import xxx;" Strings for the set of types
	 * 
	 * @param importedTypes
	 * @return
	 */
	public static Set<String> calculateImports(Set<FullyQualifiedJavaType> importedTypes) {
		StringBuilder sb = new StringBuilder();
		Set<String> importStrings = new TreeSet<String>();
		for (FullyQualifiedJavaType fqjt : importedTypes) {
			for (String importString : fqjt.getImportList()) {
				sb.setLength(0);
				sb.append("import "); //$NON-NLS-1$
				sb.append(importString);
				sb.append(';');
				importStrings.add(sb.toString());
			}
		}

		return importStrings;
	}
}
